<script setup>
import { reactive, ref,onMounted } from 'vue';
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import { User, Lock, Key } from '@element-plus/icons-vue';
/*import { adminStore, tokenStore } from '../store/store.js';
// 引入axios编写api

import captchaApi from '../api/captcha.js';*/
import loginApi from '../api/login_api.js'

const admin = ref({
  member: '',
  password: '',
  captcha: '',
  key: ''
});
onMounted(function(){
  /*localStorage.removeItem("token");*/
  // 加载验证码
  loadCaptcha();
})

// 获取路由对象
const router = useRouter();

// 验证码图片数据
const captcha = ref("");


const onSubmit = async () => {
  const res = await loginApi.login(admin.value);
  if (res) {
    localStorage.setItem("token",res);
    await router.push('/');
  }
}

// 加载验证码
const loadCaptcha=async ()=>{
    const res=await loginApi.captcha();
    admin.value.key=res.key;
    captcha.value=res.base;
}


</script>


<template>
  <div class="login-container">
    <div class="background-blur"></div>
    <div class="login-bg">
      <el-form class="form-login" :model="admin">
        <!-- 表单内容保持不变 -->
        <el-row>
          <el-col :span="10">
            <img style="width: 100px" src="@/assets/logo.png" alt="中享思途" />
          </el-col>
          <el-col :span="14">
            <h1>商户登录</h1>
          </el-col>
        </el-row>
        <el-form-item>
          <el-input v-model="admin.member"
                    placeholder="请输入账号"
                    :prefix-icon="User"
                    maxlength="16"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="admin.password"
                    placeholder="请输入密码"
                    show-password
                    :prefix-icon="Lock"
                    maxlength="16"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input v-model="admin.captcha"
                    placeholder="验证码"
                    :prefix-icon="Key"
                    maxlength="5"
                    class="input-verCode"></el-input>
          <img :src="captcha"
               class="img-verCode" @click="loadCaptcha">
        </el-form-item>
        <el-form-item>
          <el-button class="btn-login" type="success" @click="onSubmit">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>


<style scoped>.login-container {
  position: relative;
  height: 100vh;
  overflow: hidden;
}

.background-blur {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-image: url('../assets/background.png');
  background-repeat: no-repeat;
  background-position: center;
  background-size: cover;
  filter: blur(10px);
  transform: scale(1.1);
  z-index: 1;
}

.login-bg {
  position: relative;
  height: 100%;
  z-index: 2;
}

.form-login {
  width: 550px;
  padding: 30px;
  position: absolute;
  top: 30%;
  left: calc(48% - 215px);
  background-color: rgba(255, 255, 255, 0.9);
  box-shadow: 10px 10px 30px #000;
  backdrop-filter: blur(5px);
}

.btn-login {
  width: 100%;
}

h1 {
  text-align: center;
  font-size: 24px;
  margin-bottom: 18px;
  height: 50px;
  line-height: 50px;
}

.input-verCode {
  width: 160px;
}

.img-verCode {
  width: 80px;
  height: 30px;
}
</style>